import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:test_flutter/common/base/BaseRouter.dart';
import 'package:test_flutter/generated/l10n.dart';
import 'package:test_flutter/page/provider2_page.dart';
import 'package:test_flutter/page/provider_page.dart';
import 'package:test_flutter/page/screen_page.dart';
import 'package:test_flutter/page/store_page.dart';
import 'package:test_flutter/page/home_page.dart';
import 'package:test_flutter/page/localization_page.dart';
import 'package:test_flutter/page/login_page.dart';
import 'package:test_flutter/page/native_page.dart';
import 'package:test_flutter/page/navigator_page.dart';
import 'package:test_flutter/page/ui_page.dart';
import 'package:test_flutter/page/welcome_page.dart';
import 'package:test_flutter/util/db_utils.dart';
import 'package:test_flutter/util/shared_preferences_utils.dart';
import 'package:test_flutter/widget/constant_variable_demo_page.dart';
import 'package:test_flutter/widget/framework_demo_page.dart';
import 'package:test_flutter/widget/function_demo_page.dart';
import 'package:test_flutter/widget/widget_demo_page.dart';

import 'page/animation_page.dart';
import 'page/bottom_nav_page.dart';
import 'page/customview_page.dart';
import 'page/tab_nav_page.dart';

void main() async {
  /// 注意：需要添加下面的一行，才可以使用异步方法
  WidgetsFlutterBinding.ensureInitialized();

  // await DBUtils.getInstance();
  await DBUtils.init();
  await SharedPreUtils.init();

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.

  MyApp() {
    print("《《《《《《《《MyApp");
    ///设置路由导航
    BaseRouter.setRouter(FluroRouter.appRouter);
    BaseRouter.registerConfigureRoutes(BaseRouter.getRoute());
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: const [
        S.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
      ],
      supportedLocales: [
        const Locale('en', 'zh_CN'),
        ...S.delegate.supportedLocales
      ],

      ///插件目前不完善需要手动处理简繁体
      localeResolutionCallback: (locale, supportLocales) {
        ///中文 简繁体处理
        if (locale?.languageCode == 'zh') {
          if (locale?.scriptCode == 'Hant') {
            return const Locale('zh', 'HK');

            ///繁体
          } else {
            return const Locale('zh', 'CN');

            ///简体
          }
        }
        return null;
      },
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
      routes: routers,
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, this.title = ""}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  _initScreen(BuildContext context) {
    print(
        'width:${MediaQuery.of(context).size.width}, height:${MediaQuery.of(context).size.height}');
    ScreenUtil.init(context, designSize: Size(720.0, 1280.0));
    // ScreenUtil.init(
    //   BoxConstraints(
    //       maxWidth: MediaQuery.of(context).size.width,
    //       maxHeight: MediaQuery.of(context).size.height),
    //   designSize: Size(720.0, 1280.0),
    // );
    ScreenUtil().setWidth(720.0);
    ScreenUtil().radius(320);
    ScreenUtil().setSp(24.0);
  }

  @override
  Widget build(BuildContext context) {
    _initScreen(context);
    var routerList = routers.keys.toList();
    // 感觉不写new也没问题
    return new Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
        child: ListView.builder(
          itemBuilder: (context, index) {
            return InkWell(
              onTap: () {
                print(routerList[index]);
                Navigator.pushNamed(context, routerList[index]);

                ///静态路由跳转
//                  Navigator.push(context, MaterialPageRoute(builder: (context){
//                    return ConstantVariableDemoPage();
//                  }));
              },
              child: Card(
                child: Container(
                  color: Colors.white,
                  alignment: Alignment.center,
                  margin: EdgeInsets.symmetric(horizontal: 10),
                  height: 50,
                  child: Text(routerList[index].toString()),
                ),
              ),
            );
          },
          itemCount: routerList.length,
        ),
        color: Colors.yellow,
      ),
    );
  }
}

///静态路由
Map<String, WidgetBuilder> routers = {
  "999.主页面": (context) => HomePage(),
  "19.路由导航2.0": (context) => CustomViewPage(),
  "18.自定义组件": (context) => CustomViewPage(),
  "17.UI相关(WebView,toast,switch,dialog)": (context) => UIPage(),
  "16.屏幕适配": (context) => ScreenPage(),
  "15.状态管理2": (context) => ProviderPage2(),
  "14.状态管理": (context) => ProviderPage(),
  "13.本地持久化": (context) => StorePage(),
  "12.国际化": (context) => LocalizationPage(),
  "11.混合开发": (context) => NativePage(),
  "10.路由跳转": (context, {arguments}) => MyNavigatorPage(argument: arguments),
  "9.动画页面": (context) => AnimationPage(),
  "8.嵌套滑动页面": (context) => TabNavPage(),
  "7.底部导航页面": (context) => BottomNavPage(),
  "6.登录页面": (context) => LoginPage(),
  "5.欢迎页面": (context) => WelcomePage(),
  "4.framework": (context) => FrameWorkDemoPage(),
  "3.Widget": (context) => WidgetDemoPage(),
  "2.方法": (context) => FunctionDemoPage(),
  "1.常量与变量": (context) => ConstantVariableDemoPage(),
};
